package org.zcl.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.zcl.entity.TbBasicChannel;
import org.zcl.enums.ResponseCodeEnum;
import org.zcl.model.ApiResponse;
import org.zcl.service.TbBasicChannelService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;

/**
 * 频道(TbBasicChannel)表控制层
 *
 * @author Chenglin Zhu
 * @since 2022-01-06 16:49:32
 */
@RestController
@RequestMapping("tbBasicChannel")
@Api(value = "TbBasicChannelController接口", tags = "TbBasicChannelController")
public class TbBasicChannelController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private TbBasicChannelService tbBasicChannelService;

    /**
     * 分页查询所有数据
     *
     * @param page           分页对象
     * @param tbBasicChannel 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<TbBasicChannel> page, TbBasicChannel tbBasicChannel) {
        return success(this.tbBasicChannelService.page(page, new QueryWrapper<>(tbBasicChannel)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.tbBasicChannelService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param tbBasicChannel 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody TbBasicChannel tbBasicChannel) {
        return success(this.tbBasicChannelService.save(tbBasicChannel));
    }

    /**
     * 修改数据
     *
     * @param tbBasicChannel 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody TbBasicChannel tbBasicChannel) {
        return success(this.tbBasicChannelService.updateById(tbBasicChannel));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.tbBasicChannelService.removeByIds(idList));
    }

    @ApiOperation(value = "删除频道及类目", httpMethod = "POST")
    @PostMapping("/deleteBasicChannel")
    public ApiResponse<Boolean> deleteBasicChannel(@RequestBody @Valid TUnionBasicChannelDeleteVO vo) {
        Boolean aBoolean = tbBasicChannelService.deleteBasicChannel(vo);
        if (aBoolean){
            return ApiResponse.<Boolean>builder().msg(ResponseCodeEnum.DELETE_SUCCESS.getDesc()).code(ResponseCodeEnum.DELETE_SUCCESS.getCode()).data(aBoolean).build();
        }
        return ApiResponse.<Boolean>builder().msg(ResponseCodeEnum.DELETE_FAILURE.getDesc()).code(ResponseCodeEnum.DELETE_FAILURE.getCode()).data(aBoolean).build();
    }
}

